Skip to content

Latest commit

ย 

History

History
678 lines (490 loc) ยท 20.2 KB

File metadata and controls

678 lines (490 loc) ยท 20.2 KB

37์žฅ Set๊ณผ Map

37.1 Set

Set ๊ฐ์ฒด : ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์œ ์ผํ•œ ๊ฐ’๋“ค์˜ ์ง‘ํ•ฉ

์ˆ˜ํ•™์  ์ง‘ํ•™์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋ฉฐ, ๊ทธ ํŠน์„ฑ๊ณผ ์ผ์น˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ, ์—ฌ์ง‘ํ•ฉ ๋“ฑ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹ค์Œ์— ๊ธฐ์ˆ ๋œ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด์„œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

  • ๋™์ผํ•œ ๊ฐ’์„ ์ค‘๋ณตํ•˜์—ฌ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ์š”์†Œ ์ˆœ์„œ์— ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.
  • ์ธ๋ฑ์Šค๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.

37.1.1 Set ๊ฐ์ฒด์˜ ์ƒ์„ฑ

  • Set ๊ฐ์ฒด๋Š” Set ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ƒ์„ฑํ•œ๋‹ค.
const set = new Set();
console.log(set); // Set(0){}
//1. Set ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ Set ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
const set1 = new Set([1, 2, 3, 3]);
console.log(set1); // Set(3) {1, 2, 3}

const set2 = new Set("hello");
console.log(set2); // Set(4) {"h", "e", "l", "o"}
//2. ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์ค‘๋ณต๋œ ๊ฐ’์€ Set ๊ฐ์ฒด์— ์š”์†Œ๋กœ ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค.

// 3. ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” Set ๊ฐ์ฒด์˜ ํŠน์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์—์„œ ์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.
// filter๋ฅผ ์ด์šฉํ•œ ๋ฐฐ์—ด์˜ ์ค‘๋ณต ์š”์†Œ ์ œ๊ฑฐ
const uniq = (array) => array.filter((v, i, self) => self.indexOf(v) === i);
console.log(uniq([2, 1, 2, 3, 4, 3, 4])); // [2, 1, 3, 4]

// Set์„ ์‚ฌ์šฉํ•œ ๋ฐฐ์—ด์˜ ์ค‘๋ณต ์š”์†Œ ์ œ๊ฑฐ
const uniq = (array) => [...new Set(array)];
console.log(uniq([2, 1, 2, 3, 4, 3, 4])); // [2, 1, 3, 4]

37.1.2 ์š”์†Œ ๊ฐœ์ˆ˜ ํ™•์ธ

  • Set.prototype.size ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉ
const set = new Set([1, 2, 3, 3]);
console.log(set.size); // 3

size ํ”„๋กœํผํ‹ฐ๋Š” setter ํ•จ์ˆ˜ ์—†์ด getterํ•จ์ˆ˜๋งŒ ์กด์žฌํ•˜๋Š” ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ.

์ฆ‰, size ํ”„๋กœํผํ‹ฐ์— ์ˆซ์ž๋ฅผ ํ• ๋‹นํ•˜์—ฌ Set ๊ฐ์ฒด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

const set = new Set([1, 2, 3]);

console.log(Object.getOwnPropertyDescriptor(Set.prototype, "size"));
// {set: undefined, enumerable: false, configurable: true, get: ฦ’}

set.size = 10; // ๋ฌด์‹œ๋œ๋‹ค.
console.log(set.size); // 3

37.1.3 ์š”์†Œ ์ถ”๊ฐ€

  • Set.prototype.add ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ
const set = new Set();
console.log(set); // Set(0) {}

// 1. Set๊ฐ์ฒด์— ์š”์†Œ ์ถ”๊ฐ€ : add ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
set.add(1);
console.log(set); // Set(1) {1}

// 2. method chaining
set.add(2).add(3);
console.log(set); // Set(3) {1, 2, 3}

// 3. ์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ ๋ฌด์‹œ๋จ
set.add(3).add(3);
console.log(set); //Set(3) {1, 2, 3}
// 4. ์ผ์น˜ ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด NaN๊ณผ NaN์€ ๋‹ค๋ฅด๋‹ค๊ณ  ํ‰๊ฐ€๋จ. ๊ทธ๋Ÿฌ๋‚˜ Set ๊ฐ์ฒด๋Š” ์ด๋ฅผ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ. +0๊ณผ -0๋„ ๊ฐ™์€ ๋งฅ๋ฝ์œผ๋กœ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ.
const set = new Set();

console.log(NaN === NaN); // false
console.log(+0 === -0); // true

// NaN๊ณผ NaN์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
set.add(NaN).add(NaN);
console.log(set); // Set(1) {NaN}

// +0๊ณผ -0์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
set.add(+0).add(-0);
console.log(set); // Set(2) {NaN, 0}
// 5. Set ๊ฐ์ฒด๋Š” ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฐ’์„ ์š”์†Œ๋กœ ์ €์žฅ๊ฐ€๋Šฅ
const set = new Set();

set.add(1).add("a").add(true).add(undefined).add(null).add({}).add([]).add(() => {});

console.log(set); // Set(7) {1, 'a', true, undefined, null, {โ€ฆ}, Array(0), ฦ’}

37.1.4 ์š”์†Œ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ

  • Set.prototype.has ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
  • ๋ฐ˜ํ™˜๊ฐ’ : ๋ถˆ๋ฆฌ์–ธ ๊ฐ’. ์š”์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋ƒ„.
const set = new Set([1, 2, 3]);

console.log(set.has(2)); // true
console.log(set.has(4)); // false

37.1.5 ์š”์†Œ ์‚ญ์ œ

  • Set .prototoype.delete ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
  • ๋ฐ˜ํ™˜๊ฐ’ : ๋ถˆ๋ฆฌ์–ธ ๊ฐ’. ์‚ญ์ œ ์„ฑ๊ณต ์—ฌ๋ถ€.
  • ์ธ์ˆ˜ : ์‚ญ์ œํ•˜๋ ค๋Š” ์š”์†Œ๊ฐ’์„ ์ „๋‹ฌ
const set = new Set([1, 2, 3]);

// ์š”์†Œ 2๋ฅผ ์‚ญ์ œํ•œ๋‹ค.
set.delete(2);
console.log(set); // Set(2) {1, 3}

// ์š”์†Œ 1์„ ์‚ญ์ œํ•œ๋‹ค.
set.delete(1);
console.log(set); // Set(1) {3}

// ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์— ๋Œ€ํ•œ ์‚ญ์ œ๋Š” ๋ฌด์‹œ๋จ
set.delete(0);
console.log(set); // Set(1) {3}

// delete๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ method chaining์ด ๋ถˆ๊ฐ€๋Šฅ
set.delete(1).delete(2); // TypeError: set.delete(...).delete is not a function

37.1.6 ์š”์†Œ ์ผ๊ด„ ์‚ญ์ œ

  • Set.prototype.clear ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
  • ๋ฐ˜ํ™˜๊ฐ’ : undefined
const set = new Set([1, 2, 3]);

set.clear();
console.log(set); // Set(0) {}

37.1.7 ์š”์†Œ ์ˆœํšŒ

  • Set.prototype.forEach ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
  • ์ธ์ˆ˜ : ์ฝœ๋ฐฑํ•จ์ˆ˜์™€ forEach๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋‚ด๋ถ€์—์„œ this๋กœ ์‚ฌ์šฉ๋  ๊ฐ์ฒด(์˜ต์…˜)๋ฅผ ์ „๋‹ฌ
  • ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์ธ์ˆ˜ : 1-ํ˜„์žฌ ์ˆœํšŒ์ค‘์ธ ์š”์†Œ๊ฐ’, 2-ํ˜„์žฌ ์ˆœํšŒ์ค‘์ธ ์š”์†Œ๊ฐ’, 3-ํ˜„์žฌ ์ˆœํšŒ์ค‘์ธ Set ๊ฐ์ฒด ์ง€์ฒด. (์ฒซ๋ฒˆ์งธ, ๋‘๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ ๋™์ผํ•œ ์ด์œ ๋Š” ๋‹จ์ง€ Array.prototype.forEach๋ฉ”์„œ๋“œ์™€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ต์ผํ•˜๊ธฐ ์œ„ํ•จ.)
const set = new Set([1, 2, 3]);

set.forEach((v, v2, set) => console.log(v, v2, set));
/*
1 1 Set(3) {1, 2, 3}
2 2 Set(3) {1, 2, 3}
3 3 Set(3) {1, 2, 3}
*/
  • Set ๊ฐ์ฒด๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”. ๋”ฐ๋ผ์„œ ๋‹ค์Œ ๋™์ž‘์ด ๊ฐ€๋Šฅํ•จ
    • for...of๋ฌธ์œผ๋กœ ์ˆœํšŒ ๊ฐ€๋Šฅ.
    • ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•
    • ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ณ๋ง
const set = new Set([1, 2, 3]);

// Set ๊ฐ์ฒด๋Š” Set.prototype์˜ Symbol.iterator ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋‹ค.
console.log(Symbol.iterator in set); // true

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Set ๊ฐ์ฒด๋Š” for...of ๋ฌธ์œผ๋กœ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.
for (const value of set) {
  console.log(value); // 1 2 3
}

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Set ๊ฐ์ฒด๋Š” ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
console.log([...set]); // [1, 2, 3]

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Set ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
const [a, ...rest] = set;
console.log(a, rest); // 1, [2, 3]

console.log(...set); // [1, 2, 3]
  • Set ๊ฐ์ฒด๋Š” ์š”์†Œ์˜ ์ˆœ์„œ์— ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ์•Š์ง€๋งŒ, Set ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋Š” ์ˆœ์„œ๋Š” ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฅธ๋‹ค.

37.1.8 ์ง‘ํ•ฉ์—ฐ์‚ฐ

  • ๊ต์ง‘ํ•ฉ

    // ๋ฐฉ๋ฒ•1
    Set.prototype.intersection = function (set) {
      const result = new Set();
    
      for (const value of set) {
        // 2๊ฐœ์˜ set์˜ ์š”์†Œ๊ฐ€ ๊ณตํ†ต๋˜๋Š” ์š”์†Œ์ด๋ฉด ๊ต์ง‘ํ•ฉ์˜ ๋Œ€์ƒ์ด๋‹ค.
        if (this.has(value)) result.add(value);
      }
    
      return result;
    };
    
    //๋ฐฉ๋ฒ•2
    Set.prototype.intersection = function (set) {
      return new Set([...this].filter((v) => set.has(v)));
    };
    
    const setA = new Set([1, 2, 3, 4]);
    const setB = new Set([2, 4]);
    
    // setA์™€ setB์˜ ๊ต์ง‘ํ•ฉ
    console.log(setA.intersection(setB)); // Set(2) {2, 4}
    // setB์™€ setA์˜ ๊ต์ง‘ํ•ฉ
    console.log(setB.intersection(setA)); // Set(2) {2, 4}
  • ํ•ฉ์ง‘ํ•ฉ

    //๋ฐฉ๋ฒ•1
    Set.prototype.union = function (set) {
      // this(Set ๊ฐ์ฒด)๋ฅผ ๋ณต์‚ฌ
      const result = new Set(this);
    
      for (const value of set) {
        // ํ•ฉ์ง‘ํ•ฉ์€ 2๊ฐœ์˜ Set ๊ฐ์ฒด์˜ ๋ชจ๋“  ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์ง‘ํ•ฉ์ด๋‹ค. ์ค‘๋ณต๋œ ์š”์†Œ๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.
        result.add(value);
      }
    
      return result;
    };
    
    //๋ฐฉ๋ฒ•2
    Set.prototype.union = function (set) {
      return new Set([...this, ...set]);
    };
    
    const setA = new Set([1, 2, 3, 4]);
    const setB = new Set([2, 4]);
    
    // setA์™€ setB์˜ ํ•ฉ์ง‘ํ•ฉ
    console.log(setA.union(setB)); // Set(4) {1, 2, 3, 4}
    // setB์™€ setA์˜ ํ•ฉ์ง‘ํ•ฉ
    console.log(setB.union(setA)); // Set(4) {2, 4, 1, 3}
  • ์ฐจ์ง‘ํ•ฉ

    //๋ฐฉ๋ฒ•1
    Set.prototype.difference = funct\ion (set) {
      // this(Set ๊ฐ์ฒด)๋ฅผ ๋ณต์‚ฌ
      const result = new Set(this);
    
      for (const value of set) {
        // ์ฐจ์ง‘ํ•ฉ์€ ์–ด๋Š ํ•œ์ชฝ ์ง‘ํ•ฉ์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ํ•œ์ชฝ ์ง‘ํ•ฉ์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์ง‘ํ•ฉ์ด๋‹ค.
        result.delete(value);
      }
    
      return result;
    };
    
    //๋ฐฉ๋ฒ•2
    Set.prototype.difference = function (set) {
      return new Set([...this].filter(v => !set.has(v)));
    };
    
    const setA = new Set([1, 2, 3, 4]);
    const setB = new Set([2, 4]);
    
    // setA์— ๋Œ€ํ•œ setB์˜ ์ฐจ์ง‘ํ•ฉ
    console.log(setA.difference(setB)); // Set(2) {1, 3}
    // setB์— ๋Œ€ํ•œ setA์˜ ์ฐจ์ง‘ํ•ฉ
    console.log(setB.difference(setA)); // Set(0) {}
  • ๋ถ€๋ถ„์ง‘ํ•ฉ๊ณผ ์ƒ์œ„์ง‘ํ•ฉ

    //๋ฐฉ๋ฒ•1
    // this๊ฐ€ subset์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
    Set.prototype.isSuperset = function (subset) {
      for (const value of subset) {
        // superset์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ subset์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธ
        if (!this.has(value)) return false;
      }
    
      return true;
    };
    
    //๋ฐฉ๋ฒ•2
    // this๊ฐ€ subset์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
    Set.prototype.isSuperset = function (subset) {
      const supersetArr = [...this];
      return [...subset].every((v) => supersetArr.includes(v));
    };
    
    const setA = new Set([1, 2, 3, 4]);
    const setB = new Set([2, 4]);
    
    // setA๊ฐ€ setB์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
    console.log(setA.isSuperset(setB)); // true
    // setB๊ฐ€ setA์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
    console.log(setB.isSuperset(setA)); // false

37.2 Map

  • Map ์€ ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ปฌ๋ ‰์…˜์ด๋‹ค.

  • ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ์ฐจ์ด์ ์ด ์žˆ๋‹ค.

    ๊ตฌ๋ถ„ ๊ฐ์ฒด Map ๊ฐ์ฒด
    ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’ ๋ฌธ์ž์—ด ๋˜๋Š” ์‹ฌ๋ฒŒ ๊ฐ’ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  ๊ฐ’
    ์ดํ„ฐ๋Ÿฌ๋ธ” X O
    ์š”์†Œ ๊ฐœ์ˆ˜ ํ™•์ธ Object.keys(obj).length map.size

37.2.1 Map ๊ฐ์ฒด์˜ ์ƒ์„ฑ

  • Map ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ƒ์„ฑ
const map = new Map();
console.log(map); // Map(0) {}

Map ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ Map๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ. ์ด๋•Œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์€ ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋ผ์•ผํ•จ

const map1 = new Map([
  ["key1", "value1"],
  ["key2", "value2"],
]);
console.log(map1); // Map(2) {"key1" => "value1", "key2" => "value2"}

const map2 = new Map([1, 2]); // TypeError: Iterator value 1 is not an entry object

Map ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ์ดํ„ฐ๋Ÿฌ๋ธ”์— ์ค‘๋ณต๋œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ฐ‘์‹ฑ ๋ฎ์–ด์”Œ์–ด์ง. ์ฆ‰, Map ๊ฐ์ฒด์—๋Š” ์ค‘๋ณต๋œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ์š”์†Œ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์—†๋‹ค.

const map = new Map([
  ["key1", "value1"],
  ["key1", "value2"],
]);
console.log(map); // Map(1) {"key1" => "value2"}

37.2.2 ์š”์†Œ ๊ฐœ์ˆ˜ ํ™•์ธ

  • Map.prototype.size ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉ

    const map = new Map([
      ["key1", "value1"],
      ["key2", "value2"],
    ]);
    console.log(map.size); // 2

    size ํ”„๋กœํผํ‹ฐ๋Š” setter ํ•จ์ˆ˜ ์—†์ด getterํ•จ์ˆ˜๋งŒ ์กด์žฌํ•˜๋Š” ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ.

    ์ฆ‰, size ํ”„๋กœํผํ‹ฐ์— ์ˆซ์ž๋ฅผ ํ• ๋‹นํ•˜์—ฌ Map ๊ฐ์ฒด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

    const map = new Map([
      ["key1", "value1"],
      ["key2", "value2"],
    ]);
    
    console.log(Object.getOwnPropertyDescriptor(Map.prototype, "size"));
    // {set: undefined, enumerable: false, configurable: true, get: ฦ’}
    
    map.size = 10; // ๋ฌด์‹œ๋œ๋‹ค.
    console.log(map.size); // 2

37.2.3 ์š”์†Œ ์ถ”๊ฐ€

  • Map.prototype.set ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ

    const map = new Map();
    console.log(map); // Map(0) {}
    
    map.set("key1", "value1");
    console.log(map); // Map(1) {"key1" => "value1"}
  • method chaining ๊ฐ€๋Šฅ

    const map = new Map();
    
    map.set("key1", "value1").set("key2", "value2");
    
    console.log(map); // Map(2) {"key1" => "value1", "key2" => "value2"}
  • ์ค‘๋ณต๋œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ์š”์†Œ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ์ค‘๋ณต๋œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๊ฐ’์ด ๋ฎ์–ด์”Œ์›Œ์ง. ์—๋Ÿฌ๋ฐœ์ƒX

    const map = new Map();
    
    map.set("key1", "value1").set("key1", "value2");
    
    console.log(map); // Map(1) {"key1" => "value2"}
  • ์ผ์น˜ ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด NaN๊ณผ NaN์€ ๋‹ค๋ฅด๋‹ค๊ณ  ํ‰๊ฐ€๋จ. ๊ทธ๋Ÿฌ๋‚˜ Map ๊ฐ์ฒด๋Š” ์ด๋ฅผ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ. +0๊ณผ -0๋„ ๊ฐ™์€ ๋งฅ๋ฝ์œผ๋กœ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ.

    const map = new Map();
    
    console.log(NaN === NaN); // false
    console.log(0 === -0); // true
    
    // NaN๊ณผ NaN์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
    map.set(NaN, "value1").set(NaN, "value2");
    console.log(map); // Map(1) { NaN => 'value2' }
    
    // +0๊ณผ -0์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
    map.set(0, "value1").set(-0, "value2");
    console.log(map); // Map(2) { NaN => 'value2', 0 => 'value2' }
  • Map ๊ฐ์ฒด๋Š” ํ‚ค ํƒ€์ž…์— ์ œํ•œ์ด ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  ๊ฐ’์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    const map = new Map();
    
    const lee = { name: "Lee" };
    const kim = { name: "Kim" };
    
    // ๊ฐ์ฒด๋„ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    map.set(lee, "developer").set(kim, "designer");
    
    console.log(map);
    // Map(2) { {name: "Lee"} => "developer", {name: "Kim"} => "designer" }

37.2.4 ์š”์†Œ ์ทจ๋“

  • Map.prototype.get ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ

  • ์ธ์ˆ˜ : ํ‚ค๋ฅผ ์ „๋‹ฌ

  • ๋ฐ˜ํ™˜ : ์ธ์ˆ˜๋กœ์ „๋‹ฌํ•œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜. ์—†๋‹ค๋ฉด undefined๋ฅผ ๋ฐ˜ํ™˜

    const map = new Map();
    
    const lee = { name: "Lee" };
    const kim = { name: "Kim" };
    
    map.set(lee, "developer").set(kim, "designer");
    
    console.log(map.get(lee)); // developer
    console.log(map.get("key")); // undefined

37.2.5 ์š”์†Œ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ

  • Map.prototype.has ๋ฉ”์„œ๋“œ

  • ๋ฐ˜ํ™˜ : ๋ถˆ๋ฆฌ์–ธ ๊ฐ’. ํŠน์ • ์š”์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋ƒ„

    const lee = { name: "Lee" };
    const kim = { name: "Kim" };
    
    const map = new Map([
      [lee, "developer"],
      [kim, "designer"],
    ]);
    
    console.log(map.has(lee)); // true
    console.log(map.has("key")); // false

37.2.6 ์š”์†Œ ์‚ญ์ œ

  • Map.prototype.delete ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ

  • ๋ฐ˜ํ™˜๊ฐ’ : ๋ถˆ๋ฆฌ์–ธ ๊ฐ’. ์‚ญ์ œ ์„ฑ๊ณต ์—ฌ๋ถ€.

  • ์ธ์ˆ˜ : ์‚ญ์ œํ•˜๋ ค๋Š” ํ‚ค๋ฅผ ์ „๋‹ฌ

    const lee = { name: "Lee" };
    const kim = { name: "Kim" };
    
    const map = new Map([
      [lee, "developer"],
      [kim, "designer"],
    ]);
    
    map.delete(kim);
    console.log(map); // Map(1) { {name: "Lee"} => "developer" }

    ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด ์—๋Ÿฌ์—†์ด ๋ฌด์‹œ๋จ.

    const map = new Map([["key1", "value1"]]);
    
    // ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค 'key2'๋กœ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ ค ํ•˜๋ฉด ์—๋Ÿฌ์—†์ด ๋ฌด์‹œ๋œ๋‹ค.
    map.delete("key2");
    console.log(map); // Map(1) {"key1" => "value1"}

    delete๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ method chaining์ด ๋ถˆ๊ฐ€๋Šฅ

    const lee = { name: "Lee" };
    const kim = { name: "Kim" };
    
    const map = new Map([
      [lee, "developer"],
      [kim, "designer"],
    ]);
    
    map.delete(lee).delete(kim); // TypeError: map.delete(...).delete is not a function

37.2.7 ์š”์†Œ ์ผ๊ด„ ์‚ญ์ œ

  • Map.prototype.clear ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ

    const lee = { name: "Lee" };
    const kim = { name: "Kim" };
    
    const map = new Map([
      [lee, "developer"],
      [kim, "designer"],
    ]);
    
    map.clear();
    console.log(map); // Map(0) {}

37.2.8 ์š”์†Œ ์ˆœํšŒ

  • Map.prototype.forEach ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ

  • ์ธ์ˆ˜ : ์ฝœ๋ฐฑํ•จ์ˆ˜์™€ forEach๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋‚ด๋ถ€์—์„œ this๋กœ ์‚ฌ์šฉ๋  ๊ฐ์ฒด(์˜ต์…˜)๋ฅผ ์ „๋‹ฌ

  • ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์ธ์ˆ˜ : 1-ํ˜„์žฌ ์ˆœํšŒ์ค‘์ธ ์š”์†Œ๊ฐ’, 2-ํ˜„์žฌ ์ˆœํšŒ์ค‘์ธ ์š”์†Œํ‚ค, 3-ํ˜„์žฌ ์ˆœํšŒ์ค‘์ธ Map๊ฐ์ฒด ์ง€์ฒด.

    const lee = { name: "Lee" };
    const kim = { name: "Kim" };
    
    const map = new Map([
      [lee, "developer"],
      [kim, "designer"],
    ]);
    
    map.forEach((v, k, map) => console.log(v, k, map));
    /*
    developer {name: "Lee"} Map(2) {
      {name: "Lee"} => "developer",
      {name: "Kim"} => "designer"
    }
    designer {name: "Kim"} Map(2) {
      {name: "Lee"} => "developer",
      {name: "Kim"} => "designer"
    }
    */
  • Map ๊ฐ์ฒด๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”. ๋”ฐ๋ผ์„œ ๋‹ค์Œ ๋™์ž‘์ด ๊ฐ€๋Šฅํ•จ

    • for...of๋ฌธ์œผ๋กœ ์ˆœํšŒ ๊ฐ€๋Šฅ.
    • ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•
    • ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ณ๋ง
    const lee = { name: "Lee" };
    const kim = { name: "Kim" };
    
    const map = new Map([
      [lee, "developer"],
      [kim, "designer"],
    ]);
    
    // Map ๊ฐ์ฒด๋Š” Map.prototype์˜ Symbol.iterator ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋‹ค.
    console.log(Symbol.iterator in map); // true
    
    // ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Map ๊ฐ์ฒด๋Š” for...of ๋ฌธ์œผ๋กœ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.
    for (const entry of map) {
      console.log(entry); // [{name: "Lee"}, "developer"]  [{name: "Kim"}, "designer"]
    }
    
    // ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Map ๊ฐ์ฒด๋Š” ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
    console.log([...map]);
    // [[{name: "Lee"}, "developer"], [{name: "Kim"}, "designer"]]
    
    // ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Map ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
    const [a, b] = map;
    console.log(a, b); // [{name: "Lee"}, "developer"]  [{name: "Kim"}, "designer"]

Map ๊ฐ์ฒด๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ๋™์‹œ์— ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

  • Map.prototype.keys : ์š”์†Œํ‚ค๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
  • Map.prototype.values : ์š”์†Œ๊ฐ’์„ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
  • Map.prototype.entries : ์š”์†Œํ‚ค์™€ ์š”์†Œ๊ฐ’์„๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
const lee = { name: "Lee" };
const kim = { name: "Kim" };

const map = new Map([
  [lee, "developer"],
  [kim, "designer"],
]);

// Map.prototype.keys๋Š” Map ๊ฐ์ฒด์—์„œ ์š”์†Œํ‚ค๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
for (const key of map.keys()) {
  console.log(key); // {name: "Lee"} {name: "Kim"}
}

// Map.prototype.values๋Š” Map ๊ฐ์ฒด์—์„œ ์š”์†Œ๊ฐ’์„ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
for (const value of map.values()) {
  console.log(value); // developer designer
}

// Map.prototype.entries๋Š” Map ๊ฐ์ฒด์—์„œ ์š”์†Œํ‚ค์™€ ์š”์†Œ๊ฐ’์„ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
for (const entry of map.entries()) {
  console.log(entry); // [{name: "Lee"}, "developer"]  [{name: "Kim"}, "designer"]
}

๋ฉ”์„œ๋“œ ์š”์•ฝ

์ด๋ฆ„ ๊ธฐ๋Šฅ ๋น„๊ณ 
Set.prototype.size ํ”„๋กœํผํ‹ฐ ์š”์†Œ ๊ฐœ์ˆ˜ ํ™•์ธ getterํ•จ์ˆ˜๋งŒ ์กด์žฌ
Set.prototype.add ์š”์†Œ ์ถ”๊ฐ€ method chaining ๊ฐ€๋Šฅ
Set.prototype.has ์š”์†Œ ์กด์žฌ ํ™•์ธ ๋ฐ˜ํ™˜๊ฐ’: boolean
Set .prototoype.delete ์š”์†Œ ์‚ญ์ œ ๋ฐ˜ํ™˜๊ฐ’: boolean(method chaing ๋ถˆ๊ฐ€)
Set.prototype.clear ์š”์†Œ ์ผ๊ด„ ์‚ญ์ œ ๋ฐ˜ํ™˜๊ฐ’: undefined
Set.prototype.forEach ์š”์†Œ ์ˆœํšŒ
์ด๋ฆ„ ๊ธฐ๋Šฅ ๋น„๊ณ 
Map.prototype.size ํ”„๋กœํผํ‹ฐ ์š”์†Œ ๊ฐœ์ˆ˜ ํ™•์ธ getterํ•จ์ˆ˜๋งŒ ์กด์žฌ
Map.prototype.add ์š”์†Œ ์ถ”๊ฐ€ method chaining ๊ฐ€๋Šฅ
Map.prototype.has ์š”์†Œ ์กด์žฌ ํ™•์ธ ๋ฐ˜ํ™˜๊ฐ’: boolean
Map .prototoype.delete ์š”์†Œ ์‚ญ์ œ ๋ฐ˜ํ™˜๊ฐ’: boolean(method chaing ๋ถˆ๊ฐ€)
Map.prototype.clear ์š”์†Œ ์ผ๊ด„ ์‚ญ์ œ ๋ฐ˜ํ™˜๊ฐ’: undefined
Map.prototype.forEach ์š”์†Œ ์ˆœํšŒ
Map.prototype.keys ์š”์†Œํ‚ค๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
Map.prototype.values ์š”์†Œ๊ฐ’์„ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
Map.prototype.entries ์š”์†Œํ‚ค์™€ ์š”์†Œ๊ฐ’์„๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜